package cn.group.controller;

import cn.group.domain.Course;
import cn.group.domain.Faculty;
import cn.group.service.CourseService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping("/showCourse")
    public  String getCourseShow(){
        return "course";
    }
    @RequestMapping("/getCourseList")
    @ResponseBody
    public Map<String,Object>getCourseList() {
        List<Course>courseList=courseService.queryAllCourse();
        System.out.println(courseList);
        Map<String,Object>result=new HashMap<>();
        result.put("code",0);
        result.put("courseList",courseList);
        result.put("message","查询课程成功");
        return result;
    }

    @RequestMapping("/getListByPage")
    @ResponseBody
    public Map<String,Object> getListByPage(Integer currentPage,Integer pageSize){
        PageInfo<Course> pageInfo=courseService.getCourseByPage(currentPage,pageSize);
        System.out.println(pageInfo.getList());

        Map<String,Object>result=new HashMap<>();
        result.put("code",0);
        result.put("courseList",pageInfo.getList());
        result.put("totalPage",pageInfo.getPages());
        result.put("message","获取课程数据成功");
        return result;
    }

    @RequestMapping("/getOneCourse")
    @ResponseBody
    public Map<String,Object> getOneCourse(Integer id){
       Course course=courseService.queryCourseById(id);
        Map<String,Object>result=new HashMap<>();
        if (course==null){
            result.put("code",-1);
            result.put("message","获取课程数据失败");
        }else {
            result.put("code",0);
            result.put("courseList",course);
            result.put("message","获取课程数据成功");
        }
        return result;
    }
    @RequestMapping("/ChangOne")
    @ResponseBody
    public  Map<String,Object>ChangeOne(Course course){
        Boolean bl=courseService.modifyOne(course);
        Map<String,Object>result=new HashMap<>();
        if (bl==true){
            result.put("code",0);
            result.put("message","编辑课程成功");
        }else if(bl==false){
            courseService.insertOneCourse(course);
            result.put("code",0);
            result.put("message","添加课程成功");
        }else {
            result.put("code",-1);
            result.put("message","操作失败");
        }
        return result;
    }
    @RequestMapping("/deleteOne")
    @ResponseBody
    public  Map<String,Object>deleteOne(Course course){
        boolean bl=courseService.deleteOne(course);
        Map<String ,Object>result=new HashMap<>();
        if (bl==true){
            result.put("code",0);
            result.put("message","删除成功");
        }else {
            result.put("code",-1);
            result.put("message","删除失败");
        }
        return result;
    }
}
